const Koa = require('koa');
const app = new Koa();
const router = require('./routers/index.js');
const { koaBody } = require('koa-body');
const decodeTokenMiddleware = require('./middleware/decodeTokenMiddleware.js');

// middleware
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFieldsSize: 200 * 1024 * 1024
    }
}));
app.use(decodeTokenMiddleware)

// routers
app.use(router.routes());

const run = (port, callback) => {
    const server = app.listen(port, () => {
        console.log(`服务器已运行在端口：${port}`)
        callback && callback(app);
    });
    return server;
}

module.exports = {
    app,
    run
};